﻿using System;
using System.Drawing;
using System.Windows.Forms;
using RSPJ.Function;
using RSPJ.Struct;
using System.Collections.Generic;

namespace RSPJ.Windows
{
    public partial class AvailableBandsList : Form
    {
        private string GrayFile, RFile, GFile, BFile;
        private int GrayBand, RBand, GBand, BBand;
        Point GrayP, RP, GP, BP;

        public AvailableBandsList()
        {
            InitializeComponent();
        }

        private void menuOpenImageFile_Click(object sender, EventArgs e)
        {
            funcRW data = new funcRW();
            data.open_imagefile();
        }

        private void AvailableBandsList_Shown(object sender, EventArgs e)
        {
            treeBands.Nodes.Clear();
            for (int i = 0; i < GlobalData.fileList.Count; i++)
            {
                TreeNode node = new TreeNode(GlobalData.fileList[i].Name);
                treeBands.Nodes.Add(node);
                for (int j = 1; j <= GlobalData.fileList[i].Head.Bands; j++)
                {

                    TreeNode lnode = new TreeNode("Band " + j);
                    node.Nodes.Add(lnode);
                }
            }
            treeBands.ExpandAll();
            treeBands.Focus();
        }

        private void menuCloseAllFiles_Click(object sender, EventArgs e)
        {
            GlobalData.fileList = new List<Files>();
        }

        // 保证R、G、B三个选项只有一个选中
        private void rbtnR_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnR.Checked)
            {
                rbtnG.Checked = false;
                rbtnB.Checked = false;
            }
        }

        private void rbtnG_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnG.Checked)
            {
                rbtnR.Checked = false;
                rbtnB.Checked = false;
            }
        }

        private void rbtnB_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnB.Checked)
            {
                rbtnR.Checked = false;
                rbtnG.Checked = false;
            }
        }

        // 窗口关闭后，将窗口状态记录改成关闭
        private void AvailableBandsList_FormClosed(object sender, FormClosedEventArgs e)
        {
            WindowsStatus.availableBandsList = false;
        }

        // 刷新列表，暂时采用这个办法解决窗口打开状态下的文件列表更新
        private void menuRefresh_Click(object sender, EventArgs e)
        {
            AvailableBandsList_Shown(sender, e);
        }

        // 加载图像
        private void btnLoad_Click(object sender, EventArgs e)
        {
            Files file = new Files();
            file.Head.Bands = 3;

            if (rbtnGrayScale.Checked && txtBand.Text != "")
            {
                file.Head.Lines = GrayP.X;
                file.Head.Samples = GrayP.Y;
                file.Name = txtBand.Text;
                file.IntArray = new double[3, file.Head.Lines, file.Head.Samples];
                for (int i = 0; i < GlobalData.fileList.Count; i++)
                {
                    if (GrayFile == GlobalData.fileList[i].Name)
                    {
                        for (int p = 0; p < GrayP.X; p++)
                        {
                            for (int q = 0; q < GrayP.Y; q++)
                            {
                                file.IntArray[0, p, q] = file.IntArray[1, p, q] =
                                    file.IntArray[2, p, q] = GlobalData.fileList[i].IntArray[GrayBand - 1, p, q];
                            }
                        }
                    }
                }

                // 进行重分级
                funcStretch fs = new funcStretch();
                file = fs.Reclass(file);
                //均衡化
                file = fs.Equalization(file);

                int[,,] tmp = new int[3, file.Head.Lines, file.Head.Samples];
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < file.Head.Lines; j++)
                        for (int k = 0; k < file.Head.Samples; k++)
                            tmp[i, j, k] = Convert.ToInt32(file.IntArray[i, j, k]);

                Display display = new Display(txtBand.Text, tmp, file.Head.Lines, file.Head.Samples);
                display.Show();
            }
            else if (rbtnRGBColor.Checked && txtR.Text != "" && txtG.Text != "" && txtB.Text != "")
            {
                if (RP == GP && GP == BP)
                {
                    file.Head.Lines = RP.X;
                    file.Head.Samples = RP.Y;
                    file.Name = txtR.Text + ":" + txtG.Text + ":" + txtB.Text;
                    file.IntArray = new double[3, file.Head.Lines, file.Head.Samples];
                    for (int i = 0; i < GlobalData.fileList.Count; i++)
                    {
                        if (RFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < RP.X; p++)
                            {
                                for (int q = 0; q < RP.Y; q++)
                                {
                                    file.IntArray[0, p, q] = GlobalData.fileList[i].IntArray[RBand - 1, p, q];
                                }
                            }
                        }
                        if (GFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < RP.X; p++)
                            {
                                for (int q = 0; q < RP.Y; q++)
                                {
                                    file.IntArray[1, p, q] = GlobalData.fileList[i].IntArray[GBand - 1, p, q];
                                }
                            }
                        }
                        if (BFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < RP.X; p++)
                            {
                                for (int q = 0; q < RP.Y; q++)
                                {
                                    file.IntArray[2, p, q] = GlobalData.fileList[i].IntArray[BBand - 1, p, q];
                                }
                            }
                        }
                    }

                    // 进行均衡化
                    funcStretch fs = new funcStretch();
                    file = fs.Reclass(file);
                    file = fs.Equalization(file);

                    int[,,] tmp = new int[3, file.Head.Lines, file.Head.Samples];
                    for (int i = 0; i < 3; i++)
                        for (int j = 0; j < file.Head.Lines; j++)
                            for (int k = 0; k < file.Head.Samples; k++)
                                tmp[i, j, k] = Convert.ToInt32(file.IntArray[i, j, k]);

                    Display display = new Display(txtR.Text + ":" + txtG.Text + ":" + txtB.Text, tmp, file.Head.Lines, file.Head.Samples);
                    display.Show();
                }
                else
                {
                    MessageBox.Show("请选择行列数相同的波段！", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        // 选中之后将选择的波段获取
        private void treeBands_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeBands.SelectedNode.Level == 1)
            {
                treeBands.PathSeparator = " ";
                int a = treeBands.SelectedNode.Level;
                string[] tmp = treeBands.SelectedNode.FullPath.Split(' ');
                if (rbtnGrayScale.Checked)
                {
                    GrayFile = tmp[0];
                    GrayBand = Convert.ToInt32(tmp[2]);
                    txtBand.Text = "Band " + GrayBand + ":" + GrayFile;
                    for (int i = 0; i < GlobalData.fileList.Count; i++)
                    {
                        if (GrayFile == GlobalData.fileList[i].Name)
                        {
                            GrayP.X = GlobalData.fileList[i].Head.Lines;
                            GrayP.Y = GlobalData.fileList[i].Head.Samples;
                        }
                    }
                }
                else
                {
                    if (rbtnR.Checked)
                    {
                        RFile = tmp[0];
                        RBand = Convert.ToInt32(tmp[2]);
                        txtR.Text = "Band " + RBand + ":" + RFile;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (RFile == GlobalData.fileList[i].Name)
                            {
                                RP.X = GlobalData.fileList[i].Head.Lines;
                                RP.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }

                        rbtnG.Checked = true;
                    }
                    else if (rbtnG.Checked)
                    {
                        GFile = tmp[0];
                        GBand = Convert.ToInt32(tmp[2]);
                        txtG.Text = "Band " + GBand + ":" + GFile;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (GFile == GlobalData.fileList[i].Name)
                            {
                                GP.X = GlobalData.fileList[i].Head.Lines;
                                GP.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }
                        rbtnB.Checked = true;
                    }
                    else
                    {
                        BFile = tmp[0];
                        BBand = Convert.ToInt32(tmp[2]);
                        txtB.Text = "Band " + BBand + ":" + BFile;
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (BFile == GlobalData.fileList[i].Name)
                            {
                                BP.X = GlobalData.fileList[i].Head.Lines;
                                BP.Y = GlobalData.fileList[i].Head.Samples;
                            }
                        }
                        rbtnR.Checked = true;
                    }
                }
            }
        }

        // 保证RGB选项和Gray选项只选中一个
        private void rbtnGrayScale_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnGrayScale.Checked)
            {
                rbtnRGBColor.Checked = false;
                pnlGray.Enabled = true;
                pnlGray.Visible = true;
                pnlRGB.Enabled = false;
                pnlRGB.Visible = false;
                pnlGray.BringToFront();
                treeBands.Focus();
            }
            else
            {
                rbtnRGBColor.Checked = true;
                pnlRGB.Enabled = true;
                pnlRGB.Visible = true;
                pnlGray.Enabled = false;
                pnlGray.Visible = false;
                pnlRGB.BringToFront();
                treeBands.Focus();
            }
        }

        private void rbtnRGBColor_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnRGBColor.Checked)
            {
                rbtnGrayScale.Checked = false;
            }
            else
            {
                rbtnGrayScale.Checked = true;
            }
        }
    }
}
